﻿using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using LibQuestionIsPhone.Models;
using LibQuestionIsPhone.QuizPhoneService;

namespace LibQuestionIsPhone.Managers
{
    public class ManagerServices
    {
        public event EventHandler TipologiaComplete;

        public void GetDatiService(int version)
        {
            if (DataFlowObject.ListTipologiaTest == null)
            {
                QuizServiceClient client = new QuizServiceClient();
                client.GetTipologieAndDifficoltaCompleted += client_GetTipologieAndDifficoltaCompleted;
                client.GetTipologieAndDifficoltaAsync(new GetTipologieAndDifficoltaRequest() { Version = version });
            }
        }

        void client_GetTipologieAndDifficoltaCompleted(object sender, GetTipologieAndDifficoltaCompletedEventArgs e)
        {
            DataFlowObject.ListTipologiaTest = new ObservableCollection<TipologiaTest>();
            foreach (var item in e.Result.ListTipologie)
                DataFlowObject.ListTipologiaTest.Add(new TipologiaTest()
                {
                    IDTest = item.IdTipologiaTest,
                    NomeTest = item.Descrizione
                });
            DataFlowObject.ListDifficolta = new ObservableCollection<Difficolta>();
            foreach (var item in e.Result.ListDifficolta)
                DataFlowObject.ListDifficolta.Add(new Difficolta()
                {
                    IDDifficolta = item.IDDifficolta,
                    NumMaxDomande = item.NumMaxDomande,
                    TempoMaxDomande = item.TempoMaxDomande,
                    Punteggio = item.Punteggio,
                    Titolo = item.Titolo
                });
            DataFlowObject.Version = e.Result.Version;
            DataFlowObject.UpdateDifficolta = e.Result.UpdateDifficolta;
            DataFlowObject.UpdateDomande = e.Result.UpdateDomande;
            DataFlowObject.UpdateTipologia = e.Result.UpdateTipologie;
            DataFlowObject.InfoBacheca = e.Result.InfoBacheca;

            if (TipologiaComplete != null)
                TipologiaComplete(null, null);
        }

        public ObservableCollection<TipologiaTest> getTipologiaTest()
        {
            if (DataFlowObject.ListTipologiaTest == null)
                GetDatiService(4);
            return DataFlowObject.ListTipologiaTest;
        }

    }
}
